﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Framework.MVVM.Model;
using Config.IService;
using Log.IService;
using Common.IService;
using Framework.MVC;
using Log.Model;
using Config.Model;
using Common.Service;
using Log.Service;
using Config.Service;

namespace CollectionCenter.Models
{
    [ServiceAttribute(typeof(IConfigService), typeof(ConfigService), "Instance")]
    [ServiceAttribute(typeof(ILogService), typeof(LogService), "Instance")]
    [ServiceAttribute(typeof(ICodeService), typeof(CodeService), "Instance")]
    public class GetConfigModel<T> : AsyncModelBase, IWriteLog, IGetConfig<T>
    {
        #region Field

        private const string m_error = "config000";
        private const string m_complete = "config001";

        #endregion Field

        #region AsyncModelBase

        protected override void OnInit(Type type)
        {
            OnBeginRequest();
            this.GetConfig(m_error, m_complete);
        }

        #endregion AsyncModelBase

        #region IWriteLog

        public void OnWriteLogComplete(string eventCode)
        {
            OnEndRequest();
        }

        public void OnWriteLogError(string eventCode)
        {
            OnEndRequest();
        }

        #endregion IWriteLog

        #region IGetConfig

        public string ConfigName { get; set; }

        public void OnGetConfigComplete(string key, T entry)
        {
            OnComplete(key, entry);
            this.Log(key, ConfigName);
        }

        public void OnGetConfigError(string key)
        {
            this.Log(key, ConfigName);
        }

        #endregion IGetConfig
    }
}